﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;

namespace GetSmartIDEList.Lib.ORM
{
    public class OrmUtil<T>
    {
        /// <summary>
        /// T的类型实例
        /// </summary>
        private Type dType;

        /// <summary>
        /// T的属性表
        /// </summary>
        private PropertyInfo[] properties;

        public OrmUtil()
        {
            dType = typeof(T);
            properties = dType.GetProperties();
        }

        public SqlConnection Connection { get; set; }

        /// <summary>
        /// 检查连接是否可用
        /// </summary>
        /// <returns></returns>
        private bool CheckConnection()
        {
            return Connection?.State == ConnectionState.Open;
        }

        public int Insert(T entity)
        {
            if (!CheckConnection()) return -1;// 检查状态
            var insert = $"insert into {dType.Name}({string.Join(",", properties.Select(t => t.Name))})";
            var values = properties.Select(p => p.GetValue(entity));
            var commandText = $"{insert} values('{string.Join("','", values)}')";

            var command = Connection.CreateCommand();
            command.CommandText = commandText;
            var result = command.ExecuteNonQuery();
            return result;
        }

        public int Update(T entity, string keyName, object keyValue)
        {
            if (!CheckConnection()) return -1;
            var setValues = properties.ToDictionary(p => p.Name, p => $"'{p.GetValue(entity)}'");
            var setSql = string.Join(",", setValues.Select(pair => $"{pair.Key}='{pair.Value}'"));
            var sql = $"update {dType.Name} set {setSql} where {keyName} = '{keyValue}'";
            var command = Connection.CreateCommand();
            command.CommandText = sql;
            return command.ExecuteNonQuery();
        }

        public int Delete(T entity)
        {
            if (!CheckConnection()) return -1;
            var querySet = properties.Select(p => $"{p.Name} = '{p.GetValue(entity)}'");
            var sql = $"delete from {dType.Name} where {string.Join(" and ", querySet)}";
            var command = Connection.CreateCommand();
            command.CommandText = sql;
            return command.ExecuteNonQuery();
        }
    }
}
